package com.dididoctor.patient.Activity.Usercentre.Consume;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/8.
 */
public class UserConsumePresenter extends BasePresenter {

    private UserConsumeView view;

    public UserConsumePresenter(Context context, UserConsumeView view) {
        super(context, view);
        this.view = view;
    }

    public void getConsume(int pageIndex) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("pageIndex", Util.toString(pageIndex));
        params.put("pageSize", "10");
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.paylist, params, new MyHttpResponseHandler() {
            private List<Consume> consumes = new ArrayList<Consume>();
            private List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    maps = (List<Map<String, Object>>) response.getObject("paylist");
                    for (Map<String, Object> map : maps) {
                        Consume consume = new Consume();
                        consume.setPayId(Util.toString(map.get("payId")));
                        consume.setMoney((Double) map.get("money") + "");
                        consume.setName(Util.toString(map.get("name")));
                        consume.setPayTime(Util.toString(map.get("payTime")));
                        consume.setStatus(Util.toString(map.get("status")));
                        consume.setPayType(Util.toString(map.get("payType")));
                        consume.setOrderId(Util.toString(map.get("orderId")));
                        consumes.add(consume);
                    }
                    view.getConsume(consumes);
                } else {
                    view.getConsume(consumes);
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getConsumefail();
            }
        });
    }

    /**
     * 申请微信退款
     *
     * @param payId
     */
    public void payback(String payId, String orderId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("orderId", orderId);
        params.put("payId", payId);
        BusinessClient.post(ConstantValue.payback, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.paybackw();
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.paybackwfail();
            }
        });
    }

    /**
     * 申请支付宝退款
     *
     * @param payId
     */
    public void alipayback(String payId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("payId", payId);
        BusinessClient.post(ConstantValue.alipayback, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.paybackz();
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.paybackzfail();
            }
        });
    }
}
